package com.bw.common;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 13096
 */
@SpringBootConfiguration
public class MyWebMvcConfig implements WebMvcConfigurer {


    /**
     * 全局解决跨域
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //允许跨域访问的路径
//        registry.addMapping("/**")
//                //允许跨域访问的源
//                .allowedOrigins("*")
//                //允许的请求方法  也可以写 *
//                .allowedMethods("POST", "GET", "PUT", "DELETE", "OPTIONS")
//                //预检间隔时间  每次请求会发起两次 第一次是预检请求 前锋 先去看是否允许跨域
//                .maxAge(16800)
//                //允许头部设置
//                .allowedHeaders("*")
//                //是否发送cookie
//                .allowCredentials(true);
//    }
    
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.json());
        return template;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
